local redis ={}
function redis.testEnable()
    local redis = require "resty.redis"
    local red = redis:new()
    red:set_timeout(100) -- 100ms

    local ok, err = red:connect(ngx.var.redhost, ngx.var.redport)
    if not ok then
        ngx.log(ngx.ERR, "failed to connect to redis: ", err)
        return nil
    end

    if ngx.var.redauth then
        local ok, err = red:auth(ngx.var.redauth)
        if not ok then
            ngx.log(ngx.ERR,"failed to authenticate: ", err)
            return nil
        end
    end
    local ok, err = red:set_keepalive(60000, 1000)
    if not ok then
        ngx.log(ngx.ERR, "failed to set_keepalive: ", err)
    end
    return "OK"
end
function redis.redkey(kid,reddb)
    -- get key from redis
    -- nil  (something went wrong, let the request pass)
    -- null (no such key, reject the request)
    -- key  (the key)

    local redis = require "resty.redis"
    local red = redis:new()
    red:set_timeout(500) -- 100ms

    local ok, err = red:connect(ngx.var.redhost, ngx.var.redport)
    if not ok then
        ngx.log(ngx.ERR, "failed to connect to redis: ", err)
        return nil
    end

    if ngx.var.redauth then
        local ok, err = red:auth(ngx.var.redauth)
        if not ok then
            ngx.log(ngx.ERR,"failed to authenticate: ", err)
            return nil
        end
    end
    reddb = reddb or ngx.var.reddb
    if ngx.var.reddb then
        local ok, err = red:select(ngx.var.reddb)
        if not ok then
            ngx.log(ngx.ERR,"failed to select db: ", ngx.var.reddb, " ", err)
            return nil
        end
    end

    local res, err = red:get(kid)
    if not res then
        ngx.log(ngx.ERR, "failed to get kid: ", kid ,", ", err)
        return nil
    end

    if res == ngx.null then
        ngx.log(ngx.ERR, "key ", kid, " not found")
        return nil
    end

    local ok, err = red:set_keepalive(60000, 1000)
    if not ok then
        ngx.log(ngx.ERR, "failed to set_keepalive: ", err)
    end

    return res
end
function redis.setvalue(key,value,reddb)
    local redis = require "resty.redis"
    local red = redis:new()
    red:set_timeout(100) -- 100ms

    local ok, err = red:connect(ngx.var.redhost, ngx.var.redport)
    if not ok then
        ngx.log(ngx.ERR, "failed to connect to redis: ", err)
        return nil
    end

    if ngx.var.redauth then
        local ok, err = red:auth(ngx.var.redauth)
        if not ok then
            ngx.log(ngx.ERR,"failed to authenticate: ", err)
            return nil
        end
    end
    reddb = reddb or ngx.var.reddb
    if ngx.var.reddb then
        local ok, err = red:select(ngx.var.reddb)
        if not ok then
            ngx.log(ngx.ERR,"failed to select db: ", ngx.var.reddb, " ", err)
            return nil
        end
    end
    red:set(key,value);
    local ok, err = red:set_keepalive(60000, 1000)
    if not ok then
        ngx.log(ngx.ERR, "failed to set_keepalive: ", err)
    end
end

function redis.delkey(key,reddb)
    local redis = require "resty.redis"
    local red = redis:new()
    red:set_timeout(100) -- 100ms

    local ok, err = red:connect(ngx.var.redhost, ngx.var.redport)
    if not ok then
        ngx.log(ngx.ERR, "failed to connect to redis: ", err)
        return nil
    end

    if ngx.var.redauth then
        local ok, err = red:auth(ngx.var.redauth)
        if not ok then
            ngx.log(ngx.ERR,"failed to authenticate: ", err)
            return nil
        end
    end
    reddb = reddb or ngx.var.reddb
    if ngx.var.reddb then
        local ok, err = red:select(ngx.var.reddb)
        if not ok then
            ngx.log(ngx.ERR,"failed to select db: ", ngx.var.reddb, " ", err)
            return nil
        end
    end
    red:del(key);
    local ok, err = red:set_keepalive(60000, 1000)
    if not ok then
        ngx.log(ngx.ERR, "failed to set_keepalive: ", err)
    end
end
return redis;

